//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pt;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.MemberInfoDao;
import com.aidong.dao.wills.PtCoachCodeDao;
import com.aidong.dao.wills.PtContractDao;
import com.aidong.dao.wills.PtTransferDao;
import com.aidong.model.wills.MemberInfo;
import com.aidong.model.wills.PtContract;
import com.aidong.model.wills.PtTransfer;
import com.aidong.web.controller.member.PayController;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"ptTransfer"})
public class PtTransferController extends PayController {
    @Resource
    PtTransferDao ptTransferDao;
    @Resource
    PtCoachCodeDao ptCoachCodeDao;
    @Resource
    PtContractDao ptContractDao;
    @Resource
    MemberInfoDao memberInfoDao;

    public PtTransferController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return new ModelAndView("pt/transfer/list");
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "shopCode",required = false) String shopCode, @RequestParam(value = "cotractCode",required = false) String cotractCode, @RequestParam(value = "memberCode",required = false) String memberCode, @RequestParam(value = "coachCode",required = false) String coachCode, @RequestParam(value = "dateFrom",required = false) String dateFrom, @RequestParam(value = "dateTo",required = false) String dateTo, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        this.addConditionShopCode(cd, "b.shopCode", shopCode);
        cd.eq("b.cotractCode", cotractCode);
        cd.eq("b.memberCode", memberCode);
        if(!this.chkNull(coachCode)) {
            ConditionMap mv = new ConditionMap(1);
            mv.eq("b.coachCode", coachCode);
            mv.eq("b.newCoachCode", coachCode);
            cd.addSub(mv);
        }

        if(!this.chkNull(dateFrom)) {
            cd.ge("b.createTime", this.parseDate(dateFrom, "yyyy-MM-dd"));
        }

        if(!this.chkNull(dateTo)) {
            cd.le("b.createTime", this.parseDate(dateTo + " 23:59:59", "yyyy-MM-dd HH:mm:ss"));
        }

        this.ptTransferDao.queryPageList(pg, cd);
        ModelAndView mv1 = new ModelAndView("pt/transfer/_list");
        mv1.addObject("list", pg.getResultList());
        mv1.addObject("pg", pg);
        return mv1;
    }

    @RequestMapping({"add"})
    public ModelAndView add() {
        List coachList = this.ptCoachCodeDao.getShopCoachList(this.getLoginShopCode());
        ModelAndView mv = new ModelAndView("pt/transfer/add");
        mv.addObject("coachList", coachList);
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("transfer") PtTransfer transfer) {
        if(this.chkNull(transfer.getContractCode())) {
            return this.getResult(false, "请输入合同号");
        } else if(this.chkNull(transfer.getNewMemberCode())) {
            return this.getResult(false, "请输入新会员号");
        } else {
            PtContract oldContract = this.ptContractDao.getByContractCode(transfer.getContractCode());
            if(oldContract == null) {
                return this.getResult(false, "私教合同号错误!");
            } else if(oldContract.getStatus().intValue() == 1 && oldContract.getOutTime() == null) {
                if(oldContract.getBespeakQuantity().intValue() >= oldContract.getQuantity().intValue()) {
                    return this.getResult(false, "合同已经没有剩余课时");
                } else if(oldContract.getBespeakQuantity() != oldContract.getClearQuantity()) {
                    return this.getResult(false, "有预约课程没有核销,不能转店");
                } else if(oldContract.getEndTime().getTime() < System.currentTimeMillis()) {
                    return this.getResult(false, "合同已过有效期,不能转店");
                } else if(oldContract.getMemberCode().equalsIgnoreCase(transfer.getNewMemberCode())) {
                    return this.getResult(false, "转入会员不能和原会员相同");
                } else {
                    MemberInfo member = this.memberInfoDao.getById(transfer.getNewMemberCode());
                    if(member != null && !this.chkNull(member.getCardCode())) {
                        int outQuan = oldContract.getQuantity().intValue() - oldContract.getBespeakQuantity().intValue();
                        double outAmount = (double)Math.round((double)outQuan * oldContract.getFactPrice().doubleValue() / (double)oldContract.getQuantity().intValue());
                        transfer.setShopCode(this.getLoginShopCode());
                        transfer.setContractCode(oldContract.getContractCode());
                        transfer.setMemberCode(oldContract.getMemberCode());
                        transfer.setPtCode(oldContract.getPtCode());
                        transfer.setCoachCode(oldContract.getCoachCode());
                        transfer.setOutQuantity(Integer.valueOf(outQuan));
                        transfer.setOutAmount(Double.valueOf(outAmount));
                        transfer.setNewMemberCode(member.getMemberCode());
                        transfer.setCreateUser(this.getLoginUserCode());
                        transfer.setCreateTime(new Date());
                        transfer.setUpdateTime(new Date());
                        PtContract contract = new PtContract();
                        contract.setContractCode(oldContract.getContractCode());
                        contract.setShopCode(oldContract.getShopCode());
                        contract.setMemberCode(transfer.getNewMemberCode());
                        contract.setPtCode(oldContract.getPtCode());
                        contract.setCoachCode(oldContract.getCoachCode());
                        contract.setEarnestId((Integer)null);
                        contract.setEarnestAmount(Double.valueOf(0.0D));
                        contract.setInAmount(Double.valueOf(outAmount));
                        contract.setPayAmount(Double.valueOf(0.0D));
                        contract.setSaleType("4");
                        contract.setSaleDate(new Date());
                        contract.setQuantity(Integer.valueOf(outQuan));
                        contract.setFactPrice(Double.valueOf(outAmount));
                        contract.setBespeakQuantity(Integer.valueOf(0));
                        contract.setClearQuantity(Integer.valueOf(0));
                        contract.setBeginTime(this.getTrimDate(new Date()));
                        contract.setEndTime(oldContract.getEndTime());
                        contract.setStatus(Integer.valueOf(1));
                        contract.setCommissionDate(new Date());
                        contract.setReceivePaper(Integer.valueOf(0));
                        contract.setSignStatus(Integer.valueOf(2));
                        contract.setPrintLabel(Integer.valueOf(0));
                        contract.setOldContractCode(oldContract.getContractCode());
                        String saleClassify = "2";
                        Integer pn = this.ptContractDao.getCountByMemberCode(contract.getMemberCode());
                        if(!this.chkNullZ(pn)) {
                            saleClassify = "1";
                        }

                        Date dt = this.memberContractDao.getFirstDateByMemberCode(contract.getMemberCode());
                        if(this.getAccountDate(new Date()).equals(this.getAccountDate(dt))) {
                            saleClassify = "3";
                        }

                        contract.setSaleClassify(saleClassify);
                        contract.setCreateUser(this.getLoginUserCode());
                        contract.setCreateTime(new Date());
                        contract.setUpdateTime(new Date());
                        contract.setReviewStatus(Integer.valueOf(0));
                        contract.setSalesCode(oldContract.getSalesCode());
                        contract.setSalesCode2(oldContract.getSalesCode2());

                        try {
                            this.ptTransferDao.addTransfer(transfer, contract);
                            this.addUserLog("私教转让", "合同号:" + transfer.getContractCode() + ",会员:" + transfer.getMemberCode());
                        } catch (Exception var12) {
                            this.log.error("add transfer", var12);
                            return this.getResult(false, "保存发生错误");
                        }

                        return this.getResult(true, "保存完成");
                    } else {
                        return this.getResult(false, "转入会员错误");
                    }
                }
            } else {
                return this.getResult(false, "无效合同");
            }
        }
    }
}
